package com.kk.score.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kk.score.entity.DepartmentEntity;
import com.kk.score.service.UnitService;
import com.kk.score.service.UserService;
import com.kk.score.utils.PageUtils;
import com.kk.score.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 
 *
 * @author KK
 * @email 409592048@qq.com
 * @date 2022-11-02 12:28:00
 */
@RestController
@RequestMapping("/unit")
public class UnitController {

    @Autowired
    private UnitService unitService;

    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = unitService.queryPage(params);

        return R.ok().put("data", page);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R save(@RequestBody DepartmentEntity department){
        if (unitService.getOne(new LambdaQueryWrapper<DepartmentEntity>().eq(DepartmentEntity::getName, department.getName())) != null) {
            return R.error("此部门已存在！");
        } else {
            department.setIs_unit(1);
            unitService.save(department);
            return R.ok();
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R update(@RequestBody DepartmentEntity department){
        DepartmentEntity one = unitService.getOne(new LambdaQueryWrapper<DepartmentEntity>().eq(DepartmentEntity::getName, department.getName()));
        if (!Objects.isNull(one) && !one.getUid().equals(department.getUid())) {
            return R.error("此部门已存在！");
        } else {
            unitService.updateById(department);
            return R.ok();
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R delete(@RequestBody String[] uids){
        if (uids == null || uids.length == 0){
            return R.error("请选择需要移到不可用的部门");
        }
        List<DepartmentEntity> departmentEntities = new ArrayList<>();
        Arrays.asList(uids).forEach(i->{
            departmentEntities.add(new DepartmentEntity(i,0));
        });
        unitService.updateBatchById(departmentEntities);
        //将这些部门下的user移动到不可用
        return R.ok().put("data",userService.deleteByDepartmentUids(uids,0));
    }

    /**
     * 彻底删除
     */
    @PostMapping("/cDelete")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R cDelete(@RequestBody String[] uids){
        if (uids == null || uids.length == 0){
            return R.error("请选择需要彻底删除的部门");
        }
        List<DepartmentEntity> departmentEntities = new ArrayList<>();
        Arrays.asList(uids).forEach(i->{
            departmentEntities.add(new DepartmentEntity(i,-1));
        });
        unitService.updateBatchById(departmentEntities);
        //删除这些部门下的user
        //TODO 删除部门下的所有任务
        return R.ok().put("data",userService.deleteByDepartmentUids(uids,-1));
    }

    /**
     * 恢复
     */
    @PostMapping("/isT")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R isT(@RequestBody String[] uids) {
        if (uids == null || uids.length == 0){
            return R.error("请选择需要恢复的部门");
        }
        List<DepartmentEntity> departmentEntities = new ArrayList<>();
        Arrays.asList(uids).forEach(i->{
            departmentEntities.add(new DepartmentEntity(i,1));
        });
        unitService.updateBatchById(departmentEntities);
        return R.ok();
    }
}
